/**
 * 
 */
package rer.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import rer.model.Tarefa;
import rer.util.ConexaoJDBC_mysql;

/**
 * Programa que
 * 
 * @author Jose C M Rodrigues
 * @since 15/11/2014
 * @version 1.0
 */
public class TarefasDAO implements ITarefasDAO{

	private Statement statement;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;
	private String query;
	private Tarefa tarefa;
	private List<Tarefa> tarefas;
	private Connection conexao = ConexaoJDBC_mysql.getConnection(); 
	
	
	@Override
	public Boolean cadastrar(Tarefa tarefa) {
		
		
		try{
			
			query = "insert into db_ceirr.tarefas (nomeTarefa, descTarefa, statusTarefa) values (?, ?, 'A')";
			
			preparedStatement = conexao.prepareStatement(query);
			preparedStatement.setString(1, tarefa.getNomeTarefa());
			preparedStatement.setString(2, tarefa.getDescTarefa());
			//l
			preparedStatement.execute();
			
			return true;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}finally {
			
			try{
				preparedStatement.close();
				//System.out.println("\nStatement fechado");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("\n Erro ao fechar o objeto statement.");
			}
		}
		
		return false;
		
	}

	@Override
	public List<Tarefa> listar() {
	//	System.out.println("listar inicio");
		tarefas = null;
		
		try{
			
			query = "SELECT * FROM db_ceirr.tarefas where statusTarefa ='A'";
			statement = conexao.createStatement();
			resultSet = statement.executeQuery(query);
			
			tarefas = new ArrayList<Tarefa>();
			
			while (resultSet.next()){
				
				tarefa = new Tarefa();
				tarefa.setIdTarefa(resultSet.getInt("idTarefas"));
				tarefa.setNomeTarefa(resultSet.getString("nomeTarefa"));
				tarefa.setDescTarefa(resultSet.getString("descTarefa"));
				
				tarefas.add(tarefa);
				
			}
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}
		
		try {
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("\n Erro ao fechar o objeto preparedStatement.");
		}
		System.out.println("\nSucesso pesquisa");
		return tarefas;
		
	}

	@Override
	public Boolean editar(Tarefa tarefa) {
		System.out.println("Dados tarefa Alterados com sucesso - DAO " + tarefa.getDescTarefa() );
		try{
			
			query = "update db_ceirr.tarefas set nomeTarefa = ?, descTarefa= ? where idTarefas = ?";
			
			preparedStatement = conexao.prepareStatement(query);
			preparedStatement.setString(1, tarefa.getNomeTarefa());
			preparedStatement.setString(2, tarefa.getDescTarefa());
			preparedStatement.setInt(3, tarefa.getIdTarefa());
			
			preparedStatement.executeUpdate();
			
			System.out.println("Dados tarefa Alterados com sucesso - DAO " + tarefa.getDescTarefa() );
			return true;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		}
		
		return false;
		
	}

	@Override
	public Boolean deletar(int id) {
		System.out.println("test dele");
		try{
			
			query = "update db_ceirr.tarefas set statusTarefa = 'I' where idTarefas = ?";
			
			preparedStatement = conexao.prepareStatement(query);
			preparedStatement.setInt(1, id);
			
			preparedStatement.executeUpdate();
			
			System.out.println("\nTarefa apagada com sucesso");
			
			return true;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
			
			return false;
		}
		
	}

	@Override
	public Tarefa pesquisarTarefa(int id) {
	
		tarefa = null;
		
		try{
			
			query = "SELECT * FROM db_ceirr.tarefas WHERE tarefas.idtarefas = ?";
			preparedStatement = conexao.prepareStatement(query);
			
			preparedStatement.setInt(1, id);
			
			resultSet = preparedStatement.executeQuery();
			
			if (resultSet.next()){
				
				tarefa = new Tarefa();
				
				tarefa.setIdTarefa(resultSet.getInt("idTarefas"));
				tarefa.setNomeTarefa(resultSet.getString("nomeTarefa"));
				tarefa.setDescTarefa(resultSet.getString("descTarefa"));
								
			}
			
			return tarefa;
			
		}catch(SQLException erro){
			System.out.println(erro.getMessage());
		} finally{
		
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("\n Erro ao fechar o objeto preparedStatement.");
			}
		}
		
		System.out.println("\nSucesso pesquisa");
		
		return null;
	}

}
